﻿using System;
using System.Data;
using System.Drawing;
using System.Web.UI.WebControls;
using HiauPdm.Bll;
using HiauPdm.Web;

namespace HiauPdm.Web
{
    public partial class GysGlMx : PageBase
    {
        protected override bool IsAccess()
        {
            return IsAccess("Jsc/GysGl.aspx");
        }

        /// <summary>
        /// 加载
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack) { return; }
            BtnSave.OnClientClick = string.Format("this.disabled=true;{0};return false;", Page.ClientScript.GetPostBackEventReference(BtnSave, null));
            BindData(int.Parse(Request.QueryString["id"]), Request.QueryString["cz"]);
            Details1.Focus();
        }

        /// <summary>
        /// 绑定刷新数据
        /// </summary>
        private void BindData(int id, string sCz)
        {
            // 查询数据
            DataSet ds = BJcSj.GetGysById(id);
            if (ds != null && sCz == "tj")
            {
                if (ds.Tables[0].Rows.Count <= 0)
                {
                    ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
                }
                ds.Tables[0].Rows[0]["ID"] = 0;
                ds.Tables[0].Rows[0]["编号"] = "GYS";
                ds.Tables[0].Rows[0]["编辑人"] = (string)Session["RealName"];
                ds.Tables[0].Rows[0]["编辑时间"] = DateTime.Now;
            }
            Details1.DataSource = ds;
            Details1.DataBind();
            // 设置操作权限
            if (sCz == "ck" || !IsAccess("编辑供应商") || ds == null)
            {
                for (int i = 0; i < Details1.Rows.Count; i++)
                {
                    ((TextBox)Details1.Rows[i].Cells[1].Controls[1]).ReadOnly = true;
                    ((TextBox)Details1.Rows[i].Cells[1].Controls[1]).BackColor = Color.FromArgb(0xE7F3FF);
                }
                LblTitle.Text = "查看";
                BtnSave.Visible = false;
            }
            else if (sCz == "tj")
            {
                LblTitle.Text = "添加";
                BtnSave.Text = BtnSave.ToolTip = "添加";
                BtnSave.Visible = true;
            }
            else if (sCz == "bj")
            {
                LblTitle.Text = "编辑";
                BtnSave.Text = BtnSave.ToolTip = "保存";
                BtnSave.Visible = true;
            }
        }

        /// <summary>
        /// 保存
        /// </summary>
        protected void BtnSave_Click(object sender, EventArgs e)
        {
            if (!IsAccess("编辑供应商")) { return; }
            if (!ShowErr(BJcSj.SaveGys((int)Details1.DataKey.Value,
                ((TextBox)Details1.Rows[1].Cells[1].Controls[1]).Text,
                ((TextBox)Details1.Rows[2].Cells[1].Controls[1]).Text,
                ((TextBox)Details1.Rows[3].Cells[1].Controls[1]).Text,
                ((TextBox)Details1.Rows[4].Cells[1].Controls[1]).Text,
                ((TextBox)Details1.Rows[5].Cells[1].Controls[1]).Text,
                ((TextBox)Details1.Rows[6].Cells[1].Controls[1]).Text,
                ((TextBox)Details1.Rows[7].Cells[1].Controls[1]).Text,
                ((TextBox)Details1.Rows[8].Cells[1].Controls[1]).Text,
                ((TextBox)Details1.Rows[9].Cells[1].Controls[1]).Text,
                ((TextBox)Details1.Rows[10].Cells[1].Controls[1]).Text,
                ((TextBox)Details1.Rows[11].Cells[1].Controls[1]).Text,
                ((TextBox)Details1.Rows[12].Cells[1].Controls[1]).Text,
                ((TextBox)Details1.Rows[13].Cells[1].Controls[1]).Text,
                ((TextBox)Details1.Rows[14].Cells[1].Controls[1]).Text,
                ((TextBox)Details1.Rows[15].Cells[1].Controls[1]).Text,
                ((TextBox)Details1.Rows[16].Cells[1].Controls[1]).Text,
                (string)Session["RealName"])))
            {
                Response.Write("<script type='text/javascript'>top.returnValue=true;top.close();</script>");
                Response.End();
            }
        }
    }
}